package musicserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class MusicLibrary {
	public static HashMap<String, Artist> ArtistAlbum;
	public MusicLibrary() {
		ArtistAlbum = new HashMap<String, Artist>();
		try {
			loadfromFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	static public void getContents(File aFile) {
		try {
			BufferedReader input = new BufferedReader(new FileReader(aFile));
			try {
				String line;
				while ((line = input.readLine()) != null) {
					if (line.matches(".*\\.mp3")) {
						Scanner s = new Scanner(line);
						s.useDelimiter("\\\\");
						s.next();
						s.next();
						String art = s.next();
						if(ArtistAlbum.get(art) == null)
							ArtistAlbum.put(art, new Artist(art));
						Artist a = ArtistAlbum.get(art);
						String alb = s.next();
						Album thealbum  = a.addAlbum(alb);
						String tra = s.next();
						Music thisTrack = new Music(art, alb, tra.replaceFirst("\\d\\d ", "").replaceFirst("\\.mp3", ""));
						thealbum.addTrack(thisTrack);
					}
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		System.out.println("Finished Loading Tracks");
	}

	public void loadfromFile() throws IOException {
		File musiclist = new File("DirPrnInfo.txt");
		getContents(musiclist);
	}
	
	public ArrayList<String> getArtistList(){
		ArrayList<String> art = new ArrayList<String>();
		for (String s : ArtistAlbum.keySet()) {
			System.out.println(s);
			art.add(s);
		}
		return art;
	}
	
	public ArrayList<String> getAlbumList(String Artist){
		System.out.println("getAlbumList "+ Artist);
		Artist a  = ArtistAlbum.get(Artist);
		ArrayList<String> ret = new ArrayList<String>();
		System.out.println(a==null);
		if(a != null)
		for (Album al : a.albums) {
			ret.add(al.getName());
		}
		return ret;
	}
	
	public ArrayList<String> getTrackList(String Artist, String Album){
		Artist a  = ArtistAlbum.get(Artist);
		for (Album al : a.albums) {
			if(al.getName().equals(Album)){
				return al.getTrackNames();
			}
		}
		return null;
	}
	
	public Music getTrack(String Artist, String Album, String Track){
		Artist a  = ArtistAlbum.get(Artist);
		for (Album alb : a.albums) {
			if(alb.getName().equals(Album)){
				for (Music trac : alb.getTracks()) {
					if(trac.getTrackName().equals(Track))
						return trac;
				}
				
			}
		}
		return null;
	}
	
	public static void main(String[] args) throws IOException{
		MusicLibrary m = new MusicLibrary();
		m.loadfromFile();
		Artist a = MusicLibrary.ArtistAlbum.get("The Fratellis");
		ArrayList<Album> al = a.getAlbums();
		for (Album album : al) {
			System.out.println(album.name);
			ArrayList<Music> tr = album.getTracks();
			for (Music music : tr) {
				System.out.println(music);
			}
		}
	}


	public ArrayList<String> searchArtist(String arg0) {
		ArrayList<String> ret = new ArrayList<String>();
		for (String Artist : ArtistAlbum.keySet()) {
			if(Artist.contains(arg0)){
				ret.add(Artist);
			}
		}
		return ret;		
	}
 

}
